function checkPath() {
    if(Test-Path payload_input,payload_output){
        Write-Output "Path existed"
    }else{
        mkdir payload_input,payload_output
    }
}
function flashGo() {
    ./payload_dumper.exe
    ./fastboot flash abl_ab payload_output/abl.img
    ./fastboot flash aop_ab payload_output/aop.img
    ./fastboot flash bluetooth_ab payload_output/bluetooth.img
    ./fastboot flash boot_ab payload_output/boot.img
    ./fastboot flash devcfg_ab payload_output/devcfg.img
    ./fastboot flash dsp_ab payload_output/dsp.img
    ./fastboot flash dtbo_ab payload_output/dtbo.img
    ./fastboot flash featenabler_ab payload_output/featenabler.img
    ./fastboot flash hyp_ab payload_output/hyp.img
    ./fastboot flash imagefv_ab payload_output/imagefv.img
    ./fastboot flash keymaster_ab payload_output/keymaster.img
    ./fastboot flash modem_ab payload_output/modem.img
    ./fastboot flash qupfw_ab payload_output/qupfw.img
    ./fastboot flash tz_ab payload_output/tz.img
    ./fastboot flash uefisecapp_ab payload_output/uefisecapp.img
    ./fastboot flash vbmeta_system_ab payload_output/vbmeta_system.img
    ./fastboot flash vbmeta_ab payload_output/vbmeta.img
    ./fastboot flash vendor_boot_ab payload_output/vendor_boot.img
    ./fastboot flash xbl_config_ab payload_output/xbl_config.img
    ./fastboot flash xbl_ab payload_output/xbl.img
    if($global:codeName="ALIOTH"){
        ./fastboot flash cmnlib_ab payload_output/cmnlib.img
        ./fastboot flash cmnlib64_ab payload_output/cmnlib64.img
    }elseif ($global:codeName="HAYDN") {
        ./fastboot flash cpucp_ab payload_output/cpucp.img
        ./fastboot flash shrm_ab payload_output/shrm.img
    }
    ./fastboot reboot fastboot
    ./fastboot flash odm_a payload_output/odm.img
    ./fastboot flash system_a payload_output/system.img
    ./fastboot flash vendor_a payload_output/vendor.img
    ./fastboot flash product_a payload_output/product.img
    ./fastboot flash system_ext_a payload_output/system_ext.img
    ./fastboot erase userdata
    ./fastboot erase metadata
    ./fastboot set_active a
    ./fastboot reboot
}
function main() {
    checkPath
    if(Test-Path miui_ALIOTH_*.zip){
        Expand-Archive miui_ALIOTH_*.zip -DestinationPath payload_input -Force
        $global:codeName="ALIOTH"
        Write-Output "Your device is $codeName"
        flashGo
    }elseif (Test-Path miui_HAYDN_*.zip) {
        Expand-Archive miui_HAYDN_*.zip -DestinationPath payload_input -Force
        $global:codeName="HAYDN"
        Write-Output "Your device is $codeName"
        flashGo
    }else {
        Write-Output "^ _ ^"
    }
}
main
Remove-Item payload_input,payload_output -Recurse
Read-Host